<%--
  Created by IntelliJ IDEA.
  User: Yipin
  Date: 2016/12/1
  Time: 12:25
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../include/head.jsp">
    <jsp:param name="title" value="user_list"/>
</jsp:include>

<jsp:include page="../include/admin_header.jsp"/>

<jsp:include page="../include/admin_sidebar.jsp"/>

<div class="content-wrapper">
    <section class="content-header">
        <h4 class="page-title" style="display: inline-block"><spring:message code="label.admin.change.password"/></h4>
    </section>
    <section class="content">
        <div class="box box-primary">
            <div class="box-body">
                <div class="form-horizontal">
                    <div class="sie-padding-top">
                        <div class="form-group form-group-change">
                            <label class="col-md-5 sie-left-name"><spring:message code="label.original.password"/>:</label>

                            <div class="col-md-7">
                                <input id="old" type="password" class="form-control sie-input-border sie-input-uwh"/>
                            </div>
                        </div>
                        <div class="form-group form-group-change">
                            <label class="col-md-5 sie-left-name"><spring:message code="label.new.password"/>:</label>

                            <div class="col-md-7">
                                <input id="new" type="password" class="form-control sie-input-border sie-input-uwh"/>
                            </div>
                        </div>
                        <div class="form-group form-group-change">
                            <label class="col-md-5 sie-left-name"><spring:message code="label.confirm.password"/>:</label>

                            <div class="col-md-7">
                                <input id="verify" type="password" class="form-control sie-input-border sie-input-uwh"
                                       onblur="verifyPassword()" style="display: inline-block"/>
                                <scan id="message"></scan>
                            </div>
                        </div>
                        <div class="form-group form-group-change">
                            <div class="col-md-offset-5" style="margin-top: 79px;">
                                <a id="editPassword" class="solid-button-blue" href="javascript:;"
                                   style="margin-right: 30px"><spring:message code="label.user.save"/></a>
                                <a class="solid-button-orange" href="javascript:history.back(-1);"><spring:message code="label.user.cancel"/></a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<jsp:include page="../include/foot.jsp"/>
<script>
    $(document).ready(function () {
        $("#editPassword").click(function () {
            $.ajax({
                type: "post",
                url: "/admin/api/v1/changePassword",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    oldPassword: $("#old").val(),
                    newPassword: $("#new").val()
                }),
                success: function (data) {
                    if (data.result == "SUCCESS") {
                        window.location.replace("/admin/logout".replace("{tenant}", getKeyByUrl()));
                    } else if (data.result == "ERROR_INCORRECT_PASSWORD") {
                        swal('<spring:message code="label.password.not.available"/>', "", "error")
                    } else {
                        swal("修改失败！", "", "error")
                    }
                },
                error: function (e) {
                    console.log(e);
                    swal('<spring:message code="label.modify.fail"/>', "", "error")
                }
            })
        });
    });

    function verifyPassword() {
        $("#message").removeClass("no").html("");
        var newPassword = $("#new").val();
        var verifyPassword = $("#verify").val();
        if (newPassword !== verifyPassword) {
            $("#message").addClass("no").text("<spring:message code="label.password.not.match"/>");
            return;
        }
    }
</script>
